<template>
  <a-modal
    title="编辑用户表"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :footer="null"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="用户名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入用户名称" v-decorator="['userName', {rules: [{required: true, message: '请输入用户名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="用户手机号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入用户手机号" v-decorator="['phoneNumber', {rules: [{required: true, message: '请输入用户手机号！'}]}]" />
        </a-form-item>
        <a-form-item
          label="登录密码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入登录密码" v-decorator="['password', {rules: [{required: true, message: '请输入登录密码！'}]}]" />
        </a-form-item>
        <a-form-item
          label="密码盐"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入密码盐" v-decorator="['passwordSalt', {rules: [{required: true, message: '请输入密码盐！'}]}]" />
        </a-form-item>
        <a-form-item
          label="最近登录IP"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入最近登录IP" v-decorator="['lastLoginIp', {rules: [{required: true, message: '请输入最近登录IP！'}]}]" />
        </a-form-item>
        <a-form-item
          label="最近登录时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择最近登录时间" v-decorator="['lastLoginTime',{rules: [{ required: true, message: '请选择最近登录时间！' }]}]" @change="lastLoginTimeOnChange"/>
        </a-form-item>
        <a-form-item
          label="用户状态"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-radio-group v-decorator="['status',{rules: [{ required: true, message: '请设置数据状态' }]}]" >
            <a-radio :value="0">锁定用户</a-radio>
            <a-radio :value="1">正常用户</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          label="语言"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-radio-group v-decorator="['language',{rules: [{ required: true, message: '请设置语言' }]}]" >
            <a-radio :value="1">中文</a-radio>
            <a-radio :value="2">英语</a-radio>
            <a-radio :value="3">俄语</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          label="当前积分"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入当前积分" v-decorator="['integral', {rules: [{required: true, message: '请输入当前积分！'}]}]" />
        </a-form-item>
        <a-form-item
          label="备注信息"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入" v-decorator="['remark', {rules: [{required: true, message: '请输入！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { appUserEdit } from '@/api/modular/main/appuser/appUserManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        lastLoginTimeDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              userName: record.userName,
              phoneNumber: record.phoneNumber,
              password: record.password,
              passwordSalt: record.passwordSalt,
              lastLoginIp: record.lastLoginIp,
              status: record.status,
              language: record.language,
              integral: record.integral,
              deleteFlag: record.deleteFlag,
              remark: record.remark
            }
          )
        }, 100)
        // 时间单独处理
        if (record.lastLoginTime) {
            this.form.getFieldDecorator('lastLoginTime', { initialValue: moment(record.lastLoginTime, 'YYYY-MM-DD') })
            this.lastLoginTimeDateString = moment(record.lastLoginTime).format('YYYY-MM-DD')
        }
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.lastLoginTime = this.lastLoginTimeDateString || null
            appUserEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      lastLoginTimeOnChange(date, dateString) {
        this.lastLoginTimeDateString = dateString
      },
      handleCancel () {
        this.lastLoginTimeDateString = ''
        this.form.getFieldDecorator('lastLoginTime', { initialValue: null })
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
